package com.hw.controller;

import com.github.pagehelper.PageInfo;
import com.hw.dto.R;
import com.hw.pojo.Devices;
import com.hw.service.DevicesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * @ClassName: DevicesController
 * @Description:
 * @Author: vie0134
 * @Date: 2020-12-15 17:10
 * @Version: 1.0
 **/
@RestController
@RequestMapping("/devices")
public class DevicesController {
    @Autowired
    private DevicesService devicesService;

    @RequestMapping("/findAllDevices")
    public R findAllDevices(@RequestParam(defaultValue = "1") int page,
                            @RequestParam(defaultValue = "10") int limit,
                            String address,
                            @RequestParam(defaultValue = "-100") String status,
                            @RequestParam(defaultValue = "-100") Integer areaid,
                            @RequestParam(defaultValue = "-100") Integer categoryid) {
        PageInfo<Devices> pageInfo = devicesService.findAllDevices(page, limit, address, status, areaid, categoryid);
        return R.setSuccess(pageInfo);
    }

    @PutMapping("/device")
    public R addDevices(@RequestBody Devices devices) {
        devicesService.addDevices(devices);
        return R.setSuccess(null);
    }

    @RequestMapping("/deleteDevicesById")
    public R deleteDevicesById(Integer id) {
        devicesService.deleteDevicesById(id);
        return R.setSuccess(null);
    }

    @RequestMapping("/device")
    public R updateDevices(@RequestBody Devices devices) {
        devicesService.updateDevices(devices);
        return R.setSuccess(null);
    }

    @DeleteMapping("/device")
    public R deleteDevices(String[] ids) {
        devicesService.deleteDevicesByIds(Arrays.asList(ids));
        return  R.setSuccess(null);
    }

    @GetMapping("/device/{id}")
    public R getDeviceById(@PathVariable Integer id) {
        Devices device = devicesService.findDevicesById(id);
        return R.setSuccess(device);
    }

    @PostMapping("/command/{uuid}/{command}")
    public R sendControl(@PathVariable String uuid, @PathVariable String command, HttpServletRequest request) {
        devicesService.sendControl(uuid, command);
        return R.setSuccess(null);
    }
}
